package javablackbelt.main;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

public class Base64Encoder {

	protected final static Log logger = LogFactory.getLog(Base64Encoder.class);

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		if (args != null && args.length > 1) {
			
			if ("decode".equalsIgnoreCase(args[0])) {
				logger.info("Decoded value of '" + args[1]+ "' = " + decode(args[1]));
			}
			else if ("encode".equalsIgnoreCase(args[0])) {
				logger.info("Encoded value of '" + args[1]+ "' = " + encode(args[1]));
			}
			return;
		}
		
		printUsage();
	}
	
	private static void printUsage() {
		logger.info("\nUsage:\n(decode | encode) text");
	}

	public static String encode(String plainProp) {
		return Base64.encode(plainProp.getBytes());
	}
	
	public static String decode(String encodedProp) {
		return new String(Base64.decode(encodedProp));
	}

	
}
